package com.xinghen.security.core.validate.code.image;

import com.google.code.kaptcha.Producer;
import com.xinghen.security.core.properties.SecurityProperties;
import com.xinghen.security.core.validate.code.ValidateCode;
import com.xinghen.security.core.validate.code.ValidateCodeGenerator;
import lombok.Data;
import org.springframework.web.context.request.ServletWebRequest;

import java.awt.image.BufferedImage;

/**
 * @author xinghen
 * @ClassName: ImageValidateCodeGenerator
 * @Package: com.xinghen.security.core.validate.code.image
 * @Description 默认的图片验证码生成器
 * @create 2018-04-18 15:29
 */
@Data
public class ImageValidateCodeGenerator implements ValidateCodeGenerator {

    private SecurityProperties securityProperties;

    private Producer captchaProducer;

    @Override
    public ValidateCode generate(ServletWebRequest request) {
        String kaptchaCode = captchaProducer.createText();
        BufferedImage image = captchaProducer.createImage(kaptchaCode);
        return new ImageValidateCode(image, kaptchaCode,
                securityProperties.getCode().getImage().getExpireIn());
    }
}
